<template><div><h2 id="部署" tabindex="-1"><a class="header-anchor" href="#部署"><span>部署</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#server-requirements">服务器要求</a></li>
<li><a href="#server-configuration">服务器配置</a>
<ul>
<li><a href="#nginx">Nginx</a></li>
</ul>
</li>
<li><a href="#optimization">优化</a>
<ul>
<li><a href="#autoloader-optimization">优化自动加载器</a></li>
<li><a href="#optimizing-configuration-loading">优化配置加载</a></li>
<li><a href="#optimizing-route-loading">优化路由加载</a></li>
<li><a href="#optimizing-view-loading">优化视图加载</a></li>
</ul>
</li>
<li><a href="#debug-mode">调试模式</a></li>
<li><a href="#deploying-with-forge-or-vapor">使用 Forge / Vapor 进行部署</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>当你准备将 Laravel 应用程序部署到生产环境时，你可以做一些重要的事情来确保应用程序尽可能高效地运行。本文将会提供几个范本以使你的 Laravel 应用部署妥当。</p>
<h2 id="服务器要求" tabindex="-1"><a class="header-anchor" href="#服务器要求"><span>服务器要求</span></a></h2>
<p>Laravel 框架有一些系统要求。你应该确保你的 Web 服务器具有以下最低 PHP 版本和扩展：</p>
<ul>
<li>PHP &gt;= 8.1</li>
<li>Ctype PHP 扩展</li>
<li>cURL PHP 扩展</li>
<li>DOM PHP 扩展</li>
<li>Fileinfo PHP 扩展</li>
<li>Filter PHP 扩展</li>
<li>Hash PHP 扩展</li>
<li>Mbstring PHP 扩展</li>
<li>OpenSSL PHP 扩展</li>
<li>PCRE PHP 扩展</li>
<li>PDO PHP 扩展</li>
<li>Session PHP 扩展</li>
<li>Tokenizer PHP 扩展</li>
<li>XML PHP 扩展</li>
</ul>
<h2 id="服务器配置" tabindex="-1"><a class="header-anchor" href="#服务器配置"><span>服务器配置</span></a></h2>
<h3 id="nginx" tabindex="-1"><a class="header-anchor" href="#nginx"><span>Nginx</span></a></h3>
<p>如果你将应用程序部署到运行 Nginx 的服务器上，你可以将以下配置文件作为为你的 Web 服务器配置的起点。最有可能需要根据你的服务器配置自定义此文件。<strong>如果你需要管理服务器，请考虑使用官方的 Laravel 服务器管理和部署服务，如 <a href="https://forge.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Forge</a>。</strong></p>
<p>请确保像以下配置一样，你的 Web 服务器将所有请求指向应用程序的 <code v-pre>public/index.php</code> 文件。永远不要尝试将 <code v-pre>index.php</code> 文件移动到项目的根目录，因为从项目根目录为应用提供服务会将许多敏感配置文件暴露到公网。</p>
<div class="language-nginx line-numbers-mode" data-highlighter="prismjs" data-ext="nginx" data-title="nginx"><pre v-pre class="language-nginx"><code><span class="line"><span class="token directive"><span class="token keyword">server</span></span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">listen</span> <span class="token number">80</span></span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">listen</span> [::]:80</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">server_name</span> example.com</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">root</span> /srv/example.com/public</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">add_header</span> X-Frame-Options <span class="token string">"SAMEORIGIN"</span></span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">add_header</span> X-Content-Type-Options <span class="token string">"nosniff"</span></span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">index</span> index.php</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">charset</span> utf-8</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> /</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">try_files</span> <span class="token variable">$uri</span> <span class="token variable">$uri</span>/ /index.php?<span class="token variable">$query_string</span></span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> = /favicon.ico</span> <span class="token punctuation">{</span> <span class="token directive"><span class="token keyword">access_log</span> <span class="token boolean">off</span></span><span class="token punctuation">;</span> <span class="token directive"><span class="token keyword">log_not_found</span> <span class="token boolean">off</span></span><span class="token punctuation">;</span> <span class="token punctuation">}</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> = /robots.txt</span>  <span class="token punctuation">{</span> <span class="token directive"><span class="token keyword">access_log</span> <span class="token boolean">off</span></span><span class="token punctuation">;</span> <span class="token directive"><span class="token keyword">log_not_found</span> <span class="token boolean">off</span></span><span class="token punctuation">;</span> <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">error_page</span> <span class="token number">404</span> /index.php</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> ~ \.php$</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">fastcgi_pass</span> unix:/var/run/php/php8.1-fpm.sock</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">fastcgi_param</span> SCRIPT_FILENAME <span class="token variable">$realpath_root</span><span class="token variable">$fastcgi_script_name</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">include</span> fastcgi_params</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> ~ /\.(?!well-known).*</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">deny</span> all</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="优化" tabindex="-1"><a class="header-anchor" href="#优化"><span>优化</span></a></h2>
<h3 id="优化自动加载器" tabindex="-1"><a class="header-anchor" href="#优化自动加载器"><span>优化自动加载器</span></a></h3>
<p>在部署到生产环境时，请确保你正在优化 Composer 的类自动加载器映射，以便 Composer 可以快速找到适合给定类加载的文件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> <span class="token function">install</span> --optimize-autoloader --no-dev</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
除了优化自动加载器之外，你还应该始终确保在项目的源代码控制存储库中包括一个 <code v-pre>composer.lock</code> 文件。存在 <code v-pre>composer.lock</code> 文件时，可以更快地安装项目的依赖项。</p>
</blockquote>
<h3 id="优化配置加载" tabindex="-1"><a class="header-anchor" href="#优化配置加载"><span>优化配置加载</span></a></h3>
<p>在将应用程序部署到生产环境时，你应该确保在部署过中运行 <code v-pre>config:cache</code> Artisan 命令来提前对一些配置文件做一下缓存：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan config:cache</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个命令将把 Laravel 的所有配置文件合并成一个缓存文件，大大减少框在加载配置值时必须进行的文件系统访问次数。</p>
<blockquote>
<p><strong>警告</strong><br>
如果你在部署过程中执行 <code v-pre>config:cache</code> 命令，应确保仅从配置文件中调用 <code v-pre>env</code> 函数。一旦配置已被缓存，<code v-pre>.env</code> 文件将不再被加载，所有对于 <code v-pre>.env</code> 变量 env 函数的调用将返回 null。</p>
</blockquote>
<h3 id="优化路由加载" tabindex="-1"><a class="header-anchor" href="#优化路由加载"><span>优化路由加载</span></a></h3>
<p>如果你正在构建一个包含许多路由的大型应用程序，你应该确保在部署过程中运行 <code v-pre>route:cache</code> Artisan 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan route:cache</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个命令将所有路由注册缩减成单个方法调用且放入缓存文件中，提高注册大量路由时的性能。</p>
<h3 id="优化视图加载" tabindex="-1"><a class="header-anchor" href="#优化视图加载"><span>优化视图加载</span></a></h3>
<p>在将应用程序部署到生产环境时，你应该确保在部署过程中运行 <code v-pre>view:cache</code> Artisan 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan view:cache</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个命令预编译了所有的 Blade 视图，使它们不再是按需编译，因此可以提高返回视图的每个请求的性能。</p>
<h2 id="调试模式" tabindex="-1"><a class="header-anchor" href="#调试模式"><span>调试模式</span></a></h2>
<p>在 <code v-pre>config/app.php</code> 配置文件中，调试选项决定了有多少错误信息实际上会显示给用户。默认情况下，该选项设置为遵守 <code v-pre>APP_DEBUG</code> 环境变量的值，该值存储在你的应用程序的 <code v-pre>.env</code> 文件中。</p>
<p><strong>在生产环境中，这个值应该永远是 <code v-pre>false</code>。如果在生产环境中将 <code v-pre>APP_DEBUG</code> 变量的值设置为 <code v-pre>true</code>，则存在将敏感配置值暴露给应用程序最终用户的风险。</strong></p>
<h2 id="使用-forge-vapor-部署" tabindex="-1"><a class="header-anchor" href="#使用-forge-vapor-部署"><span>使用 Forge / Vapor 部署</span></a></h2>
<h4 id="laravel-forge" tabindex="-1"><a class="header-anchor" href="#laravel-forge"><span>Laravel Forge</span></a></h4>
<p>如果你还不准备好管理自己的服务器配置，或者对于配置运行一个强大的 Laravel 应用程序所需的各种服务不太熟悉，那么 <a href="https://forge.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Forge</a> 是一个非常好的选择。</p>
<p>Laravel Forge 可以在诸如Linode、AWS 等多种基础设施服务提供商上创建服务器。此外，Forge 还安装和管理构建强大的 Laravel 应用程序所需的所有工具，例如 Nginx、MySQL、Redis、Memcached、Beanstalk 等等。</p>
<blockquote>
<p><strong>注意</strong><br>
想获取 Laravel Forge 完整部署指南吗？请查看 <a href="https://bootcamp.laravel.com/deploying" target="_blank" rel="noopener noreferrer">Laravel Bootcamp</a> 和 <a href="https://laracasts.com/series/learn-laravel-forge-2022-edition" target="_blank" rel="noopener noreferrer">Laracasts 上提供的 Forge 视频系列</a>。</p>
</blockquote>
<h4 id="vapor" tabindex="-1"><a class="header-anchor" href="#vapor"><span>Vapor</span></a></h4>
<p>如果你想要一个为 Laravel 调整的完全无服务器、自动扩展的部署平台，请看看 <a href="https://vapor.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Vapor</a>。Laravel Vapor 是一个由 AWS 提供支持的基于无服务器概念的 Laravel 部署平台。在 Vapor 上启动你的 Laravel 基础架构，并爱上无服务器的可扩展简单性。Laravel Vapor 由 Laravel 的创作者进行了精细调校，以便与框架无缝协作，因此你可以像以前一样继续编写 Laravel 应用程序。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/deployment/14840" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/de...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/deployment/14840" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/de...</a></p>
</blockquote>
</div></template>


